package com.txsj.scan.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.txsj.scan.R;
import com.txsj.scan.utils.ToastUtils;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.widget.dialog.WaitDialog;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment<P extends BasePresenter> extends Fragment  {

    protected Activity activity;
    private String TAG;
    protected P presenter;
    public View rootView;;
    protected WaitDialog wd;
    protected View mTitle;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
        TAG = getClass().getSimpleName();
        Trace.i("currentPage -> " + TAG);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(layoutId(),container,false);
            presenter = providePresenter();
            init();
        }

        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        return rootView;
    }

    protected abstract int layoutId();

    public abstract void init();

    //p层
    protected abstract P providePresenter();

    /**
     * 无参数的跳转
     *
     * @param clazz 传递进来的要跳转的目的地
     */
    public void startActivity(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * Toast
     * @param msg
     */
    public void t(String msg){
        ToastUtils.t(getActivity(),msg);
    }

    public void showLoading() {
        if (null == wd) {
            wd = new WaitDialog(activity);
        }
        if (!wd.isShowing()){
            wd.show();
        }
    }

    public void showLoading(String text,boolean showLoading) {
        if (null == wd) {
            wd = new WaitDialog(activity);
            wd.setContent(text,showLoading);
        }
        if (!wd.isShowing()){
            wd.show();
        }
    }

    public void hideLoading() {
        if (wd != null && wd.isShowing()) {
            wd.dismiss();
        }
    }

}
